# @File: build.py
#-*- coding: utf-8 -*-
# __author:"super"
# @time:2025/8/15 17:55

# build.py
import os
import platform
import subprocess
import sys
from pathlib import Path

def resource_path_spec(path: str) -> str:
    """跨平台处理 PyInstaller --add-data 路径分隔符"""
    sep = ";" if platform.system() == "Windows" else ":"
    return f"{path}{sep}{path}"

def main():
    entry = "main.py"
    name = "MyWebViewApp"
    icon = "app.ico"
    backend_import = {
        "Windows": "webview.platforms.winforms",
        "Darwin": "webview.platforms.cocoa",
        "Linux": "webview.platforms.gtk"
    }

    cmd = [
        sys.executable, "-m", "PyInstaller",
        entry,
        "--name", name,
        "--onedir",      # 或改成 --onedir 方便调试
        "--windowed",      # 调试时用 --console，看错误信息
        # "--icon", icon,
        "--add-data", resource_path_spec("static"),
        "--hidden-import", "main",  # 模块名，不是文件名
        "--hidden-import", backend_import.get(platform.system(), "")
    ]

    print(">>> 运行打包命令：", " ".join(cmd))
    subprocess.run(cmd)

    out_dir = Path("dist") / (name + (".exe" if platform.system() == "Windows" else ""))
    print(f"\n✅ 打包完成！请查看 {out_dir.parent}")

if __name__ == "__main__":
    main()
